/*
** Mortem's HGE Addon 1.4
** Copyright (C) 2006-2007, Osokin 'elmortem' Makar
** http://elmortem.mirgames.ru
**
** User profile system.
*/

#ifndef _MHAPROFILE_H_
#define _MHAPROFILE_H_

#include <hge.h>
#include <wchar.h>
#include <memory.h>
#include <mhalist.h>

#ifndef BYTE
typedef unsigned char BYTE;
#endif

enum {
	MHATYPE_INT = 0,
	MHATYPE_FLOAT,
	MHATYPE_STRING,
	MHATYPE_USERDATA,
	MHATYPE_BOOL
};

struct mhaProfileVar {
	HGE_CHAR *name;
	int type;
	int iVal;
	bool bVal;
	float fVal;
	HGE_CHAR *sVal;
	void *uVal;
	int uValSize;

	mhaProfileVar(void) {
		name = 0;
		type = MHATYPE_INT;
		iVal = 0;
		bVal = false;
		fVal = 0.f;
		sVal = 0;
		uVal = 0;
		uValSize = 0;
	};
	~mhaProfileVar(void) {
		if(name && *name) delete name;
		if(type == MHATYPE_STRING && sVal && *sVal) delete sVal;
		else if(type == MHATYPE_USERDATA && uVal) delete uVal;
	}
};

struct mhaProfileUser {
	int Id;
	mhaList *Vars;

	mhaProfileUser(void) {
		Id = -1;
		Vars = new mhaList(true);
	}
	~mhaProfileUser(void) {
		delete Vars;
	}
};

class mhaProfile {
public:
	mhaProfile(void);
	~mhaProfile(void);

	void Clear(void);
	void Reset(void);
	bool Load(const HGE_CHAR *filename = 0);
	bool Save(const HGE_CHAR *filename = 0);
	void DumpToLog(void);

	void Add(int id);
	int Select(int id = -2);
	bool Is(int id = -1);
	void Remove(int id = -1);
	
	void setInt(const HGE_CHAR *varname, int val, int id = -1);
	void setBool(const HGE_CHAR *varname, bool val, int id = -1);
	void setFloat(const HGE_CHAR *varname, float val, int id = -1);
	void setString(const HGE_CHAR *varname, const HGE_CHAR *val, int id = -1);
	void setUserdata(const HGE_CHAR *varname, void *val, int size, int id = -1);
	int getInt(const HGE_CHAR *varname, int id = -1);
	bool getBool(const HGE_CHAR *varname, int id = -1);
	float getFloat(const HGE_CHAR *varname, int id = -1);
	HGE_CHAR *getString(const HGE_CHAR *varname, int id = -1);
	void *getUserdata(const HGE_CHAR *varname, int id = -1);

	void setName(const HGE_CHAR *name, int id = -1);
	HGE_CHAR *getName(int id = -1);
	
	inline void set(const HGE_CHAR *varname, int val, int id = -1) { setInt(varname, val, id); };
	inline void set(const HGE_CHAR *varname, bool val, int id = -1) { setBool(varname, val, id); };
	inline void set(const HGE_CHAR *varname, float val, int id = -1) { setFloat(varname, val, id); };
	inline void set(const HGE_CHAR *varname, const HGE_CHAR *val, int id = -1) { setString(varname, val, id); };
	inline void set(const HGE_CHAR *varname, void *val, int id = -1) { setUserdata(varname, val, id); };
	/*inline int get(const HGE_CHAR *varname, int id = -1) { getInt(varname, id); };
	inline bool get(const HGE_CHAR *varname, int id = -1) { getBool(varname, id); };
	inline float get(const HGE_CHAR *varname, int id = -1) { getFloat(varname, id); };
	inline HGE_CHAR *get(const HGE_CHAR *varname, int id = -1) { getString(varname, id); };
	inline void *get(const HGE_CHAR *varname, int id = -1) { getUserdata(varname, id); };*/

protected:
	static HGE *hge;
	mhaList *User;
	mhaProfileUser *SelUser;
	int Sel;
	HGE_CHAR FileName[260];

	mhaProfileUser *_findUser(int id);
	mhaProfileUser *_addUser(int id, const HGE_CHAR *name);
	mhaProfileVar *_findVar(mhaList *vars, const HGE_CHAR *varname);
	mhaProfileVar *_addVar(mhaList *vars, const HGE_CHAR *varname);
};

#endif